<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use \Qiniu\Auth;
use \Qiniu\Storage\BucketManager;
class UpdateAds extends Command
{
    protected $signature = "update:ads";

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "update ads";


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $rawData = file_get_contents("http://ad.51youkantou.com/sapi/api/getAds2DSP");
        $data = json_decode($rawData, true)['data'] ?? null;
        if (empty($data)) {
            $this->error("empty data in json");
        }

        $redis = app('redis')->connection('default');

        $adList = $data['adList'] ?? null;
        if (empty($adList)) {
            $this->error("no data");
            return ;
        }

        $realKey = "ads:youkantou";
        $key = "ads:youkantou:tmp";
        
        foreach ($adList as $data) {
            $id = $data['id'];
            $cpm = rand(10,20);
            unset($data['plan']);
            $redis->hSet($key, $id, json_encode($data));
            $this->info($id);
        }

        $redis->rename($key, $realKey);
    }
}